﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


namespace SqlCommandDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            ExecuteReader();
        }


        static void ExecuteReader()
        {
            string connString = ConfigurationManager.ConnectionStrings["sqlConn"].ConnectionString;
            SqlConnection conn = new SqlConnection(connString);

            SqlCommand comm = new SqlCommand();
            comm.CommandText = "SELECT TOP 10 * FROM [Products]";
            comm.Connection = conn;
            conn.Open();

            SqlDataReader reader= comm.ExecuteReader(CommandBehavior.CloseConnection);
            while (reader.Read())
            {
                Console.WriteLine(reader["ProductName"].ToString());
            }

            Console.WriteLine("关闭DataReader前,连接的状态{0}",conn.State.ToString());
            reader.Close();
            Console.WriteLine("关闭DataReader后,连接的状态{0}", conn.State.ToString());

            Console.ReadLine();
        }
    }
}
